using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace EIM_Accounting
{
    /// <summary>
    /// 录入和计算 月考勤 『合同关联考勤录入』
    /// </summary>
    public partial class EC010101 : Form
    {
        private BLL.EC010101 ec010101 = new BLL.EC010101();

        public EC010101()
        {
            InitializeComponent();
        }

        private void label3_Click(object sender, System.EventArgs e)
        {
        }

        private void EC010101_Load(object sender, System.EventArgs e)
        {
            comboBox1.DataSource = ec010101.getMemberList().Tables[0];
            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "ID";

            dateTimePicker1.MaxDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
            dateTimePicker1.Value = DateTime.Now.AddMonths(-1);

            comboBox2.DataSource = ec010101.getContractList(comboBox1.SelectedValue.ToString(), dateTimePicker1.Value).Tables[0];
            comboBox2.DisplayMember = "ContractName";
            comboBox2.ValueMember = "ID";

            string strLegalDay = ec010101.getLegalDays(dateTimePicker1.Value, comboBox1.SelectedValue.ToString());

            textBox5.Text = strLegalDay;
            //if (comboBox2.Items.Count < 1)
            //{
            //    btnSave.Enabled = false;
            //}
        }

        private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            try
            {
                int.Parse(comboBox1.SelectedValue.ToString());
            }
            catch
            {
                return;
            }
            dateTimePicker1.Value = DateTime.Now.AddMonths(-1);

            comboBox2.DataSource = ec010101.getContractList(comboBox1.SelectedValue.ToString(), dateTimePicker1.Value).Tables[0];
            comboBox2.DisplayMember = "ContractName";
            comboBox2.ValueMember = "ID";
        }

        private void btnCalcDays_Click(object sender, EventArgs e)
        {
            string strLegalDay = string.Empty;
            CM010104 cm010104 = new CM010104();
            List<int> sdf = new List<int>();
            sdf.Add(int.Parse(comboBox1.SelectedValue.ToString()));
            cm010104.staffIDList = sdf;
            cm010104.inMonth = dateTimePicker1.Value;
            cm010104.ShowDialog();

            strLegalDay = ec010101.getLegalDays(dateTimePicker1.Value, comboBox1.SelectedValue.ToString());

            textBox5.Text = strLegalDay;

            //textBox2.Text = ec010101.getLeftLegalDays(dateTimePicker1.Value, comboBox1.SelectedValue.ToString());
        }

        private void dateTimePicker1_Leave(object sender, EventArgs e)
        {
            string strLegalDay = ec010101.getLegalDays(dateTimePicker1.Value, comboBox1.SelectedValue.ToString());

            textBox5.Text = strLegalDay;
        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (double.Parse(textBox1.Text) > double.Parse(textBox5.Text))
                {
                    textBox1.Focus();
                    throw new Exception();
                }
                if (double.Parse(textBox5.Text) > 24)
                {
                    MessageBox.Show("请检查法定工作日是否正确？");
                    return;
                }
                Accounting.Model.AccountingAttendance model = new Accounting.Model.AccountingAttendance();
                model.ContractID = int.Parse(comboBox2.SelectedValue.ToString());
                model.StaffID = int.Parse(comboBox1.SelectedValue.ToString());
                model.AttendanceMonth = dateTimePicker1.Value.ToString("yyyyMM");
                model.FactDays = textBox1.Text;
                model.LegalDays = textBox5.Text;
                if (textBox4.Text.Trim() != string.Empty)
                    model.NormalAddTimes = decimal.Parse(textBox4.Text);
                if (textBox6.Text.Trim() != string.Empty)
                    model.WeekEndAddTimes = decimal.Parse(textBox6.Text);
                if (textBox7.Text.Trim() != string.Empty)
                    model.HolidayAddTimes = decimal.Parse(textBox7.Text);
                model.InsertDate = DateTime.Now.ToString("yyyyMMdd");

                Accounting.Model.AccountingAttendance oldmodel = ec010101.getModelByMonthStaff(int.Parse(comboBox2.SelectedValue.ToString()), int.Parse(comboBox1.SelectedValue.ToString()), model.AttendanceMonth);
                if (oldmodel == null)
                {
                    ec010101.addModel(model);
                }
                else
                {
                    model.ID = oldmodel.ID;
                    StringBuilder msgStr = new StringBuilder();
                    msgStr.Append("数据库中已经存在数据，是否替换？\n");
                    msgStr.Append("--------------------------------------------------------------------------------\n");
                    msgStr.Append("实际工作日：").Append(oldmodel.FactDays).Append("\n");
                    msgStr.Append("平日加班：").Append(oldmodel.NormalAddTimes).Append("\n");
                    msgStr.Append("周末加班：").Append(oldmodel.WeekEndAddTimes).Append("\n");
                    msgStr.Append("节假日加班：").Append(oldmodel.HolidayAddTimes).Append("\n");
                    if (MessageBox.Show(msgStr.ToString(), "", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                    {
                        ec010101.updateModel(model);
                    }
                }
                this.Close();
            }
            catch
            {
                MessageBox.Show("数据不正确！");
            }
        }

        private void input_float_Press(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
            {
                e.Handled = true;
            }

            if (e.KeyChar == 46)                       //小数点
            {
                string txtValue = ((System.Windows.Forms.TextBox)sender).Text;

                if (txtValue.Length <= 0)
                    e.Handled = true;           //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(txtValue, out oldf);
                    b2 = float.TryParse(txtValue + e.KeyChar.ToString(), out f);
                    if (b2 == true)
                    {
                        if (b1 == false)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }
    }
}